﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Skin2D.Control.Cursor;

namespace Skin2D.Control
{
    enum CurrsorType
    {
        Select,
        Resize,
        Rotate,
        Scale
    }
    class CursorManager
    {
        public CursorManager(PictureBox picbox)
        {
            m_picbox = picbox;
            Init();
        }

        private void Init()
        {
            m_picbox.MouseDown += new MouseEventHandler(m_picbox_MouseDown);
            m_picbox.MouseUp += new MouseEventHandler(m_picbox_MouseUp);
            m_picbox.MouseMove += new MouseEventHandler(m_picbox_MouseMove);
        }

        public void InitCursor(ToolStripButton button,CurrsorType type)
        {
            ICursor tmp = null;
            if (type == CurrsorType.Select)
                tmp = new SelectCursor(button);
            else if (type == CurrsorType.Resize)
                tmp = new ResizeCursor(button);
            else if (type == CurrsorType.Rotate)
                tmp = new RotateCursor(button);

            m_Cursors_Dic[type] = tmp;
            m_currentCursor = tmp;

            button.Tag = type;
            button.Click += new EventHandler(button_Click);

        }

        public void setCursorType(CurrsorType type)
        {
            if (m_Cursors_Dic[m_currentType] != null)
            {
                m_Cursors_Dic[m_currentType].setCheckState(CheckState.Unchecked);
            }

            m_currentType = type;

            if (m_Cursors_Dic[type] != null)
            {
                m_Cursors_Dic[type].setCheckState(CheckState.Checked);
                m_currentCursor = m_Cursors_Dic[type];
            }
        }

        //-------------------------------------------------------------------------------------//
        //                                    Event Handler                                   //
        //-------------------------------------------------------------------------------------//
        void button_Click(object sender, EventArgs e)
        {
            ToolStripButton tmp = sender as ToolStripButton;
            if (tmp!=null)
                setCursorType((CurrsorType)tmp.Tag);
        }

        void m_picbox_MouseMove(object sender, MouseEventArgs e)
        {
            if (m_currentCursor != null)
                m_currentCursor.OnMouseMove(e);
        }

        void m_picbox_MouseUp(object sender, MouseEventArgs e)
        {
            if (m_currentCursor != null)
                m_currentCursor.OnMouseUp(e);
        }

        void m_picbox_MouseDown(object sender, MouseEventArgs e)
        {
            if (m_currentCursor != null)
                m_currentCursor.OnMouseDown(e);
        }

       

        private PictureBox m_picbox;
        private ICursor m_currentCursor;
        private CurrsorType m_currentType;
        private Dictionary<CurrsorType, ICursor> m_Cursors_Dic = new Dictionary<CurrsorType,ICursor>();
    }
}
